version 14
set more off
set seed 1234
set matsize 11000

**************************************************************************************************************
* THIS ROUTINE GENERATES TABLE A.II OF WASEEM (2019) RESTAT
**************************************************************************************************************

/* PRELIMINARIES */

use				"$project_data\ExemptionCutoff_06_11.dta", clear
matrix 			input table=(0,0,0,0\0,0,0,0\0,0,0,0\0,0,0,0\0,0,0,0\0,0,0,0\0,0,0,0\0,0,0,0\			///
							 0,0,0,0\0,0,0,0\0,0,0,0\0,0,0,0\0,0,0,0\0,0,0,0\0,0,0,0\0,0,0,0\			///
							 0,0,0,0\0,0,0,0\0,0,0,0\0,0,0,0\0,0,0,0\0,0,0,0\0,0,0,0\0,0,0,0\			///
							 0,0,0,0)			
g			double z0=taxableinc
g			double z1=F1.taxableinc
g			logchange=log(z1/z0)
			
/* OBSERVATIONS */
*-----------------

/* # TAXABLEINC>0 */

local		i=2
qui			count if employee==0
local		nself=r(N)
matrix		table[`i',1]=r(N)
qui			count if employee==1
local		nwage=r(N)
matrix		table[`i',2]=r(N)
local		i=`i'+1

/* # SELF-EMPLOYMENT INCOME>0 */

qui			count if seinc>0 & seinc<. & employee==1
local		swage=(r(N)/`nwage')*100
matrix		table[`i',2]=round(`swage',0.1)
local		i=`i'+1

/* # WAGE INCOME>0 */

qui			count if wageinc>0 & wageinc<. & employee==0
local		sself=(r(N)/`nself')*100
matrix		table[`i',1]=round(`sself',0.1)
local		i=`i'+1

/* # PARTNERSHIP INCOME>0 */

qui			count if aop_owner==1 & employee==0
local		sself=(r(N)/`nself')*100
matrix		table[`i',1]=round(`sself',0.1)
qui			count if aop_owner==1 & employee==1
local		swage=(r(N)/`nwage')*100
matrix		table[`i',2]=round(`swage',0.1)
local		i=`i'+1

/* # SWITCHERS */

qui			count if switcher_se==1 
local		sself=(r(N)/`nself')*100
matrix		table[`i',1]=round(`sself',0.1)
qui			count if switcher_we==1
local		swage=(r(N)/`nwage')*100
matrix		table[`i',2]=round(`swage',0.1)
local		i=`i'+2

/* OUTCOMES */
*-------------

/* TAXABLE INCOME */ 

qui 		sum taxableinc if employee==0,d
matrix		table[`i',1]=r(mean)
matrix		table[`i'+1,1]=r(sd)
matrix		table[`i'+2,1]=r(p50)
qui 		sum taxableinc if employee==1,d 
matrix		table[`i',2]=r(mean)
matrix		table[`i'+1,2]=r(sd)
matrix		table[`i'+2,2]=r(p50)
local		i=`i'+3

/* SELF-EMPLOYMENT INCOME */

qui 		sum seinc if seinc>0 & seinc<. & employee==0,d 
matrix		table[`i',1]=r(mean)
matrix		table[`i'+1,1]=r(sd)
matrix		table[`i'+2,1]=r(p50)
qui 		sum seinc if seinc>0 & seinc<. & employee==1,d 
matrix		table[`i',2]=r(mean)
matrix		table[`i'+1,2]=r(sd)
matrix		table[`i'+2,2]=r(p50)
local		i=`i'+3

/* WAGE INCOME */

qui 		sum wageinc if wageinc>0 & wageinc<. & employee==0,d 
matrix		table[`i',1]=r(mean)
matrix		table[`i'+1,1]=r(sd)
matrix		table[`i'+2,1]=r(p50)
qui 		sum wageinc if wageinc>0 & wageinc<. & employee==1,d 
matrix		table[`i',2]=r(mean)
matrix		table[`i'+1,2]=r(sd)
matrix		table[`i'+2,2]=r(p50)
local		i=`i'+4

/* CHARACTERISTICS */
*--------------------

/* # YEARS REGISTERED */
	
g 			regdate = date(registration_date, "DMY")			
g 			regyear=year(regdate)
g 			age_reg=2009-regyear								
qui 		sum age_reg if employee==0,d
matrix		table[`i',1]=r(mean)
matrix		table[`i'+1,1]=r(sd)
qui 		sum age_reg if employee==1,d 
matrix		table[`i',2]=r(mean)
matrix		table[`i'+1,2]=r(sd)
local		i=`i'+2

/* LOCATION */

g			urban=(location_code==21 | location_code==31 | location_code==41 | 					///
			location_code==81 | location_code==151 | location_code==152 						///
			| location_code==153 | location_code==181 | location_code==71)  
qui			sum urban if employee==0,d
matrix		table[`i',1]=r(mean)
matrix		table[`i'+1,1]=r(sd)
qui 		sum urban if employee==1,d 
matrix		table[`i',2]=r(mean)
matrix		table[`i'+1,2]=r(sd)
local		i=`i'+2

/* GENDER */
	
g			female=0
replace		female=1 if form_code==12 & year<2009
replace		female=1 if gender=="F" & year>=2009
g			male=1-female
qui 		sum male if employee==0 & gender!="",d
matrix		table[`i',1]=r(mean)
matrix		table[`i'+1,1]=r(sd)
qui 		sum male if employee==1 & gender!="",d
matrix		table[`i',2]=r(mean)
matrix		table[`i'+1,2]=r(sd)
local		i=`i'+2

/* # AGE */
	
g 			birthdate = date(birth_foundation_date, "DMY")			
g 			birthyear=year(birthdate)
g 			age_birth=2009-birthyear								
qui 		sum age_birth if employee==0,d
matrix		table[`i',1]=r(mean)
matrix		table[`i'+1,1]=r(sd)
qui 		sum age_birth if employee==1,d 
matrix		table[`i',2]=r(mean)
matrix		table[`i'+1,2]=r(sd)

**********************************************************************************************************************

*********************
* COLUMNS 3-4
*********************

drop 			if z0<=80000 & employee==0
drop 			if z0>500000 & employee==0
drop 			if z0<=140000 & employee==1
drop 			if z0>700000 & employee==1
qui				sum logchange if employee==0,d
drop			if (logchange<r(p1) | logchange>r(p99)) & employee==0
qui				sum logchange if employee==1,d
drop			if (logchange<r(p1) | logchange>r(p99)) & employee==1

/* OBSERVATIONS */
*-----------------

/* # TAXABLEINC>0 */

local		i=2
qui			count if employee==0
local		nself=r(N)
matrix		table[`i',3]=r(N)
qui			count if employee==1
local		nwage=r(N)
matrix		table[`i',4]=r(N)
local		i=`i'+1

/* # SELF-EMPLOYMENT INCOME>0 */

qui			count if seinc>0 & seinc<. & employee==1
local		swage=(r(N)/`nwage')*100
matrix		table[`i',4]=round(`swage',0.1)
local		i=`i'+1

/* # WAGE INCOME>0 */

qui			count if wageinc>0 & wageinc<. & employee==0
local		sself=(r(N)/`nself')*100
matrix		table[`i',3]=round(`sself',0.1)
local		i=`i'+1

/* # PARTNERSHIP INCOME>0 */

qui			count if aop_owner==1 & employee==0
local		sself=(r(N)/`nself')*100
matrix		table[`i',3]=round(`sself',0.1)
qui			count if aop_owner==1 & employee==1
local		swage=(r(N)/`nwage')*100
matrix		table[`i',4]=round(`swage',0.1)
local		i=`i'+1

/* # SWITCHERS */

qui			count if switcher_se==1 
local		sself=(r(N)/`nself')*100
matrix		table[`i',3]=round(`sself',0.1)
qui			count if switcher_we==1
local		swage=(r(N)/`nwage')*100
matrix		table[`i',4]=round(`swage',0.1)
local		i=`i'+2

/* OUTCOMES */
*-------------

/* TAXABLE INCOME */ 

qui 		sum taxableinc if employee==0,d
matrix		table[`i',3]=r(mean)
matrix		table[`i'+1,3]=r(sd)
matrix		table[`i'+2,3]=r(p50)
qui 		sum taxableinc if employee==1,d 
matrix		table[`i',4]=r(mean)
matrix		table[`i'+1,4]=r(sd)
matrix		table[`i'+2,4]=r(p50)
local		i=`i'+3

/* SELF-EMPLOYMENT INCOME */

qui 		sum seinc if seinc>0 & seinc<. & employee==0,d 
matrix		table[`i',3]=r(mean)
matrix		table[`i'+1,3]=r(sd)
matrix		table[`i'+2,3]=r(p50)
qui 		sum seinc if seinc>0 & seinc<. & employee==1,d 
matrix		table[`i',4]=r(mean)
matrix		table[`i'+1,4]=r(sd)
matrix		table[`i'+2,4]=r(p50)
local		i=`i'+3

/* WAGE INCOME */

qui 		sum wageinc if wageinc>0 & wageinc<. & employee==0,d 
matrix		table[`i',3]=r(mean)
matrix		table[`i'+1,3]=r(sd)
matrix		table[`i'+2,3]=r(p50)
qui 		sum wageinc if wageinc>0 & wageinc<. & employee==1,d 
matrix		table[`i',4]=r(mean)
matrix		table[`i'+1,4]=r(sd)
matrix		table[`i'+2,4]=r(p50)
local		i=`i'+4

/* CHARACTERISTICS */
*--------------------

/* # YEARS REGISTERED */
	
qui 		sum age_reg if employee==0,d
matrix		table[`i',3]=r(mean)
matrix		table[`i'+1,3]=r(sd)
qui 		sum age_reg if employee==1,d 
matrix		table[`i',4]=r(mean)
matrix		table[`i'+1,4]=r(sd)
local		i=`i'+2

/* LOCATION */

qui			sum urban if employee==0,d
matrix		table[`i',3]=r(mean)
matrix		table[`i'+1,3]=r(sd)
qui 		sum urban if employee==1,d 
matrix		table[`i',4]=r(mean)
matrix		table[`i'+1,4]=r(sd)
local		i=`i'+2

/* GENDER */
	
qui 		sum male if employee==0 & gender!="",d
matrix		table[`i',3]=r(mean)
matrix		table[`i'+1,3]=r(sd)
qui 		sum male if employee==1 & gender!="",d
matrix		table[`i',4]=r(mean)
matrix		table[`i'+1,4]=r(sd)
local		i=`i'+2

/* # AGE */
	
qui 		sum age_birth if employee==0,d
matrix		table[`i',3]=r(mean)
matrix		table[`i'+1,3]=r(sd)
qui 		sum age_birth if employee==1,d 
matrix		table[`i',4]=r(mean)
matrix		table[`i'+1,4]=r(sd)

/* EXPORTING TABLE */

svmat			table
keep			tab*
keep			if _n<=25
g				c1=""
replace			c1="\underline{\textit{Observations}}" in 1
replace			c1="1. Taxable Income > 0" in 2
replace			c1="2. Self-Employment Income > 0" in 3
replace			c1="3. Wage Income > 0" in 4
replace			c1="4. Partnership Income > 0" in 5
replace			c1="5. Switchers" in 6
replace			c1="\underline{\textit{Outcomes}}" in 7
replace			c1="6. Taxable Income" in 8
replace			c1="7. Self-Employment Income" in 11
replace			c1="8. Wage Income" in 14
replace			c1="\underline{\textit{Characteristics}}" in 17
replace			c1="9. Years Registered" in 18
replace			c1="10. Location" in 20
replace			c1="11. Male" in 22
replace			c1="12. Age" in 24
forvalues		c=2/5 {
local			t=`c'-1
g				c`c'=string(table`t',"%15.2f")
}
foreach  		r in 1 7 17 {
forvalues		c=2/5 {
replace			c`c'="" in `r'
}
}
forvalues		r=8/16{
forvalues		c=2/5 {
local			t=`c'-1
replace			c`c'=string(table`t',"%15.0fc") in `r'
}
}
replace			c2="100" in 3
replace			c3="100" in 4
replace			c4="100" in 3
replace			c5="100" in 4
forvalues		c=2/5 {
local			t=`c'-1
replace			c`c'=string(table`t',"%15.0fc") in 2
}
forvalues		c=2/5 {
foreach 		r in 9 12 15{
local			t=`c'-1
replace			c`c'="("+string(table`t',"%15.0fc")+")" in `r'
}
}
forvalues		c=2/5 {
foreach 		r in 10 13 16{
local			t=`c'-1
replace			c`c'="["+string(table`t',"%15.0fc")+"]" in `r'
}
}
forvalues		c=2/5 {
foreach 		r in 19 21 23 25{
local			t=`c'-1
replace			c`c'="("+string(table`t',"%15.2f")+")" in `r'
}
}
forvalues 		s=1/4 {
g				s`s'="&"
}
g				l="\\"
replace			l="\\ \\" in 1
replace			l="\\ \\" in 6
replace			l="\\ \\" in 7
replace			l="\\ \\" in 17
replace			l="\\ \\" in 25
replace 		s3="&&"

browse			c1 s1 c2 s2 c3 s3 c4 s4 c5 l


